//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System.Collections;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Files.Components;
using RegEx = System.Text.RegularExpressions;
using System.Collections.Specialized;

namespace CommunityServer.Files.Providers
{
	public class FileGalleryBrowseableList : BrowseableList
	{
		#region IBrowseableList Members

		public override string Name
		{
			get
			{
				return FileGalleryResourceManager.GetString("Navigation_JumpDropDownList_Files_Home");
			}
		}

		public override System.Collections.ArrayList Containers
		{
			get
			{
				ArrayList containers = new ArrayList();

				ArrayList folderGroups = Folders.GetFolderGroups(true, false, false);
				foreach (Group group in folderGroups) 
				{
                        containers.Add(new FileGalleryBrowseableContainer(group, null, null));
				}

				return containers;
			}
		}

		public override object GetNode(string listKey)
		{
			if (listKey == "")
				return this;

			int id, id2;
			PostCategory pc;
			Folder f;
			Group g;
			Entry e;
			string[] ids;
			string parentCategoryIds;
			switch (listKey.Substring(0, 1))
			{
				case "C":
					ids = listKey.Split(':');
					id = int.Parse(ids[0].Substring(1));
					id2 = int.Parse(ids[1].Substring(1));

					f = Folders.GetFolder(id2);
					g = Folders.GetFolderGroup(f.GroupID, true, false);
					pc = PostCategories.GetCategory(id, f.SectionID);

					return new FileGalleryBrowseableContainer(g, f, pc);

				case "S":
					id = int.Parse(listKey.Substring(1));
					f = Folders.GetFolder(id);
					g = Folders.GetFolderGroup(f.GroupID, true, false);

					return new FileGalleryBrowseableContainer(g, f, null);

				case "G":

					id = int.Parse(listKey.Substring(1));
					g = Folders.GetFolderGroup(id, true, false);

					return new FileGalleryBrowseableContainer(g, null, null);

				case "P":

					ids = listKey.Split(':');
					id = int.Parse(ids[0].Substring(1));
					parentCategoryIds = ids[1].Substring(1);
					e = Entries.GetEntry(id);

					return new FileGalleryBrowseableItem(e, parentCategoryIds);

				default:
					return null;
			}
		}

		public override StringCollection GetPathToNode(string listKey)
		{
			StringCollection path = new StringCollection();

			if (listKey == "")
				return path;

			int id, id2;
			PostCategory pc = null;
			Folder f = null;
			Group g = null;
			Entry e = null;
			string[] ids;
			string parentCategoryIds = "";
			switch (listKey.Substring(0, 1))
			{
				case "C":
					ids = listKey.Split(':');
					id = int.Parse(ids[0].Substring(1));
					id2 = int.Parse(ids[1].Substring(1));

					f = Folders.GetFolder(id2);
					g = Folders.GetFolderGroup(f.GroupID, true, false);
					pc = PostCategories.GetCategory(id, f.SectionID);

					break;

				case "S":
					id = int.Parse(listKey.Substring(1));
					f = Folders.GetFolder(id);
					g = Folders.GetFolderGroup(f.GroupID, true, false);

					break;

				case "G":

					id = int.Parse(listKey.Substring(1));
					g = Folders.GetFolderGroup(id, true, false);

					break;

				case "P":

					ids = listKey.Split(':');
					id = int.Parse(ids[0].Substring(1));
					parentCategoryIds = ids[1].Substring(1);
					e = Entries.GetEntry(id);
					f = Folders.GetFolder(e.SectionID);
					g = Folders.GetFolderGroup(f.GroupID, true, false);

					break;

				default:
					return path;
			}

			if (g != null)
				path.Add(BrowseableList.MakeListKey(g.GroupID, -1, -1, -1, ""));

			if (f != null)
				path.Add(BrowseableList.MakeListKey(g.GroupID, f.SectionID, -1, -1, ""));

			if (pc != null)
				path.Add(BrowseableList.MakeListKey(g.GroupID, f.SectionID, pc.CategoryID, -1, ""));

			if (e != null)
				path.Add(BrowseableList.MakeListKey(g.GroupID, f.SectionID, -1, e.PostID, parentCategoryIds));

			return path;
		}

		#endregion

		protected override ApplicationType ApplicationType
		{
			get { return ApplicationType.FileGallery; }
		}
	}

	public class FileGalleryBrowseableContainer : BrowseableListContainer
	{
		private Group _group;
		private Section _section;
		private PostCategory _category;

		public FileGalleryBrowseableContainer(Group group, Section section, PostCategory category):base(group,section,category,"")
		{
			_group = group;
			_section = section;
			_category = category;
		}

		#region IBrowseableListContainer Members

		public override System.Collections.ArrayList Items
		{
			get
			{
				if (_section != null)
				{
					FileGalleryThreadQuery query = new FileGalleryThreadQuery();
					query.SectionID = _section.SectionID;
					query.PageIndex = 0;
					query.PageSize = 999999;

                        query.OnlyApproved = true;

					string parentCategories = "";
					if (_category != null)
					{
						query.CategoryID = _category.CategoryID;
						parentCategories = _category.CategoryID.ToString();
					}

					ArrayList items = new ArrayList();
					ArrayList entries = Components.Entries.GetEntries( query ).Threads;
					foreach (Entry e in entries)
					{
						items.Add(new FileGalleryBrowseableItem(e, parentCategories));
					}

					return items;
				}
				else
					return null;
			}
		}


		public override System.Collections.ArrayList Containers
		{
			get
			{
				if (_group != null)
				{
					if (_section == null)
					{
						ArrayList containers = new ArrayList();
						ArrayList folders = Folders.GetFoldersByGroupID(_group.GroupID, false, true, false);
						foreach (Folder f in folders)
						{
                            if (BrowseableList.UserCanView( f, CSContext.Current.User))
							    containers.Add(new FileGalleryBrowseableContainer(_group, f, null));
						}

						return containers;
					}

					if (_category == null)
					{
						ArrayList containers = new ArrayList();
						ArrayList categories = PostCategories.GetCategories(_section.SectionID, true, 0);
						foreach (PostCategory pc in categories)
						{
							containers.Add(new FileGalleryBrowseableContainer(_group, _section, pc));
						}

						return containers;
					}
				}

				return null;
			}
		}
 

        #endregion
	}

	public class FileGalleryBrowseableItem : BrowseableListItem
	{
		private Entry _entry;

		public FileGalleryBrowseableItem(Entry entry, string parentCategoryIds):base(entry, parentCategoryIds)
		{
			_entry = entry;
		}

		#region IBrowseableListItem Members

		public override string GetRenderHTML(NameValueCollection optionValues)
		{
			return "<a href=\"" + FileGalleryUrls.Instance().ViewEntry(_entry.Section.ApplicationKey, _entry.PostID) + "\" target=\"_blank\">" + System.Web.HttpUtility.HtmlEncode(_entry.Subject) + "</a>";				
		}

		public override string PreviewHTML
		{
			get
			{
				string imageUrl = Entry.GetPreviewImageUrl(_entry, CSContext.Current);

				if (!Globals.IsNullorEmpty(imageUrl))
				{
					return "<img src=\"" + imageUrl + "\" border=\"0\">";
				}
				else
					return System.Web.HttpUtility.HtmlEncode(this.Name);
			}
		}

		#endregion
	}

}
